package com.example.hw3.controller;

import com.example.hw3.repository.ClassRepository;
import com.example.hw3.repository.RelationRepository;
import com.example.hw3.repository.StudentRepository;
import com.example.hw3.repository.UserRepository;
import com.example.hw3.entity.User;
import com.example.hw3.entity.Course;
import com.example.hw3.entity.Student;
import com.example.hw3.entity.Relation;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Controller

public class userController {
    @Autowired
    private StudentRepository studentRepository;
    @Autowired
    private ClassRepository classRepository;
    @Autowired
    private RelationRepository relationRepository;
    @Autowired
    private UserRepository userRepository;



    //获取登录用户名字
    private String usernameToAll;

    Student studentInfo;
    @GetMapping("/studentinfo")
    public String test(Model model) {
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext()
            .getAuthentication()
            .getPrincipal();
        usernameToAll=userDetails.getUsername();
        Student s=new Student();
        model.addAttribute("user",s);
        model.addAttribute("name",usernameToAll);
        return "StudentInfo";
    }
    @GetMapping("/classtochoose")
    public String test2(Model model2, @Valid @ModelAttribute("user") Student stuinfo, Errors errors) {
        if(errors.hasErrors()) {
            return "StudentInfo";
        }
        studentRepository.save(stuinfo);
        studentInfo =stuinfo;
        System.out.println(studentInfo);
        model2.addAttribute("user",stuinfo);
        model2.addAttribute("list",classRepository.findAll());
        model2.addAttribute("name",usernameToAll);
        return "classSelect";
    }


    @GetMapping("/classtoshow")
    public String test3(Model model3, HttpServletRequest request) {
        int index = Integer.parseInt(studentInfo.getId());
        List<Course> checkList=new ArrayList<>();

        String[] classes=request.getParameterValues("classtochooses");

        for(int i=0;i<classes.length;i++){
            relationRepository.save(new Relation(i+index+6-1500000000, studentInfo.getId(),classes[i]));
        }

        for(int i=0;i<classes.length;i++)
            checkList.add(new Course(0, null, classes[i], null, null, null, 0));


        for(int i=0;i<classes.length;i++)
            System.out.println(classes[i]);
        System.out.println(studentInfo.getId());
        model3.addAttribute("userInfo", studentInfo);
        model3.addAttribute("classes",checkList);
        model3.addAttribute("name",usernameToAll);
        return "/classShowing";
    }

    @GetMapping("/classtofirst")
    public String test4(Model model4) {
        return "Home";
    }

    @GetMapping("/register")
    public String test5(Model model5, @Valid @ModelAttribute("user") User user, Errors errors) {
        if(errors.hasErrors()) {
            return "/Register";
        }
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        User user1 = new User(user.getUsername(), encoder.encode(user.getPassword()));
        userRepository.save(user1);
        System.out.println(user1);
        model5.addAttribute("name",usernameToAll);
        return "Home";
    }

    @GetMapping("/register1")
    public String test6(Model model6) {
        User u=new User();
        model6.addAttribute("user",u);
        model6.addAttribute("name",usernameToAll);
        return "/Register";
    }
}
